package com.mdd.common.util.qichacha;

import com.alibaba.fastjson.JSONObject;
import com.mdd.common.util.HttpUtils;
import com.mdd.common.util.ToolUtils;
import org.apache.commons.math3.util.Pair;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 企查查---企业三要素验证
 */
public class ECIThreeElVerifyUtil {
    private static final String url = "https://api.qichacha.com/ECIThreeElVerify/GetInfo";
    private static final String key = "cc445d17ac434e68a004cbf0fda83d34";
    private static final String secretKey = "C47188B53F5237B8276893FA538B4D2C";

    /**
     * 企查查-综合风险排查-2006
     * @param companyName   公司名称
     * @param companyCode   公司税号/公司编号（统一社会信用代码）
     * @param operName   公司法人姓名
     * @return
     */
    public static Pair<Boolean, String> GetInfo(String companyName,  String companyCode,String operName) {

        String Timespan=new Date().getTime()/1000+"";
        String Token = ToolUtils.makeMd5(key + Timespan + secretKey);
        HttpHeaders headers=new HttpHeaders();
        headers.add("Token", Token);
        headers.add("Timespan", Timespan);

        Map<String, String> header = new HashMap<>();
        header.put("key", key);
        header.put("searchKey", companyName);

        Map<String, Object> map = new HashMap<>();
        map.put("key", key);
        map.put("creditCode", companyCode);
        map.put("companyName", companyName);
        map.put("operName", operName);
        ResponseEntity<String> response = HttpUtils.sendGetParamsWithHeadersMax(url, header, map);
        String result = response.getBody();

//        StringBuffer buffer = new StringBuffer(url).append("?key=").append(key);
//        buffer.append("&searchKey=").append(companyName);
//        String result = HttpUtils.sendGetParams(buffer.toString(), map);
        System.out.println(result);
        if(result!=null && !result.isEmpty()){
            JSONObject jsonObject = JSONObject.parseObject(result);
            System.out.println(jsonObject);
            String status = jsonObject.getString("Status");
            if("200".equals(status)){
                JSONObject resultObj = jsonObject.getJSONObject("Result");
                Integer verifyResult = resultObj.getInteger("VerifyResult");
                if(verifyResult!=null && verifyResult==0){
                    return new Pair<Boolean, String>(false, "公司统一社会信用代码错误");
                }
                else if(verifyResult!=null && verifyResult==1){
                    return new Pair<Boolean, String>(true, "验证成功");
                }
                else if(verifyResult!=null && verifyResult==2){
                    return new Pair<Boolean, String>(false, "企业名称不一致");
                }
                else if(verifyResult!=null && verifyResult==3){
                    return new Pair<Boolean, String>(false, "法定代表人名称不一致");
                }
            }
        }
        return new Pair<>(false, "核验失败，请联系管理员");
    }

    public static void main(String[] args) {
        GetInfo("北京晟程网络科技有限公司", "91110101MA1AZJJJ0G", "李国梁");
    }


}
